package com.hnuit.utils;

import java.io.InputStream;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtils{
    private static SqlSessionFactory sqlSessionFactory = null;
    // 初始化sqlSessionFactory对象
    static {
        try {
            // 使用Mybatis提供的Resources类加载MyBatis的配置文件
            InputStream reader=Resources.getResourceAsStream("mybatis-config.xml");
            // 构建sqlSessionFactory工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 获取SqlSession对象的静态方法
    public static SqlSession getSession() {
        return sqlSessionFactory.openSession();
    }
}